package com.happyfamily.springboot.crowdsourcingplatform.service.weng.impl;

import com.happyfamily.springboot.crowdsourcingplatform.base.result.Results;
import com.happyfamily.springboot.crowdsourcingplatform.dao.weng.LastInfomationDao;
import com.happyfamily.springboot.crowdsourcingplatform.model.IndustryDynamics;
import com.happyfamily.springboot.crowdsourcingplatform.model.LastestInfomation;
import com.happyfamily.springboot.crowdsourcingplatform.service.weng.LastInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
@Transactional(rollbackFor = Exception.class)
public class LastInfoServiceImpl implements LastInfoService
{
	@Autowired
	LastInfomationDao lastInfomationDao;
	@Autowired
	private RedisTemplate<Object, Object> redisTemplate;

	@Override
	public Results<LastestInfomation> getAllLastInfoByPage(Integer startPosition, Integer limit)
	{
		return Results.success(lastInfomationDao.countAllLastInfos().intValue(), lastInfomationDao.getAllLastInfosByPage(startPosition, limit));
	}

	@Override
	public Results<LastestInfomation> getInfoByFuzzy(String ltitle, String getDate, Integer startPosition, Integer limit)
	{
		return Results.success(lastInfomationDao.getInfoByFuzzy(ltitle, getDate).intValue(), lastInfomationDao.getInfoByFuzzyPage(ltitle, getDate, startPosition, limit));
	}

	@Override
	public int deleteLatestInfo(Integer lid)
	{
		//设置序列化
		redisTemplate.setKeySerializer(new StringRedisSerializer());

		int delete = lastInfomationDao.deleteLatestInfo(lid);
		if (delete > 0)
		{
			doRefresh();
		}
		return delete;
	}


	@Override
	public int openLatestInfo(Integer lid)
	{
		//设置序列化
		redisTemplate.setKeySerializer(new StringRedisSerializer());

		int open = lastInfomationDao.openLatestInfo(lid);
		if (open > 0)
		{
			doRefresh();
		}
		return open;
	}

	@Override
	public int closeLatestInfo(Integer lid)
	{
		//设置序列化
		redisTemplate.setKeySerializer(new StringRedisSerializer());

		int close = lastInfomationDao.closeLatestInfo(lid);
		if (close > 0)
		{
			doRefresh();
		}
		return close;
	}


	@Override
	public int infoEdit(Integer lid, String ltitle)
	{
		//设置序列化
		redisTemplate.setKeySerializer(new StringRedisSerializer());

		int edit = lastInfomationDao.infoEdit(lid, ltitle);
		if (edit > 0)
		{
			doRefresh();
		}
		return edit;
	}

	@Override
	public List<LastestInfomation> frontGetLatestNews()
	{
		//设置序列化
		redisTemplate.setKeySerializer(new StringRedisSerializer());
		//取redis总数
		List<LastestInfomation> lastestInfomations = (List<LastestInfomation>) redisTemplate.opsForValue().get("frontGetLatestNews");
		if (null == lastestInfomations)
		{
			synchronized (this)
			{
				lastestInfomations = (List<LastestInfomation>) redisTemplate.opsForValue().get("frontGetLatestNews");
				if (null == lastestInfomations)
				{
					lastestInfomations = lastInfomationDao.frontGetLatestNews();
					redisTemplate.opsForValue().set("frontGetLatestNews", lastestInfomations);
				}
			}
		}
		return lastestInfomations;
	}

	private void doRefresh()
	{
		List<LastestInfomation> LastestInfomation = (List<LastestInfomation>) lastInfomationDao.frontGetLatestNews();
		redisTemplate.opsForValue().set("frontGetLatestNews", LastestInfomation);
	}
}
